package com.tucc.hello.leetcode.no801_1200.no852;

/**
 * @author tucc
 * @description todo
 * @package com.tucc.hello.leetcode
 * @date 2021/5/20
 **/
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] arr = {0, 1, 0};
        System.out.println(solution.peakIndexInMountainArray(arr));
    }


    public int peakIndexInMountainArray(int[] arr) {
        if (arr.length < 3) {
            return -1;
        }
        int left = 0;
        int right = arr.length;
        while (left <= right) {
            int middle = (left + right) / 2;
            if (middle == 0) {
                middle = 1;
            }
            if (middle == arr.length - 1) {
                middle = arr.length - 2;
            }
            int mLeft = middle - 1;
            int mRight = middle + 1;
            if (arr[middle] > arr[mLeft] && arr[middle] > arr[mRight]) {
                return middle;
            } else if (arr[middle] < arr[mRight]) {
                left = mRight;
            } else {
                right = mLeft;
            }
        }
        return -1;
    }

    public int peakIndexInMountainArray2(int[] arr) {
        for (int i = 1; i < arr.length - 1; i++) {
            if (arr[i] > arr[i - 1] && arr[i + 1] < arr[i]) {
                return i;
            }
        }
        return -1;
    }
}
